<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>评论</title>
    <style>
        div {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }
        table,tr,th,td{
            border: 1px solid #5fbdcb;
        }
        table{
            border-collapse: collapse;
        }
    </style>
</head>
<body>
<div>
    <table></table>
    <a href="" id="a1">写评论</a><br>
    <a href="AllStorePage.jsp" id="a2">返回</a>
</div>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    function getParam(url, name) {
        try {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = url.split('?')[1].match(reg);
            if (r != null) {
                return r[2];
            }
            return "";
        } catch (e) {
            return "";
        }
    }

    let url = window.location.href;
    let storeId = getParam(url, 'storeId');
    $(function () {
        $.ajax({
            url: "http://localhost:8080/comment.do",
            type: "post",
            data: {
                storeId: storeId,
                method: 'selectComment'
            },
            dataType: "text",
            success: function (data) {
                let parse = JSON.parse(data);
                console.log(parse);
                if (parse.length === 0) {
                    alert("该店铺没有评论")
                } else {
                    let table = document.querySelector('table');
                    for (let i = 0; i < parse.length; i++) {
                        if (parse[i].userIdentity === 2) {
                            let tr1 = document.createElement('tr');
                            table.appendChild(tr1);
                            tr1.innerText = '商家留言';
                            let tr2 = document.createElement('tr');
                            table.appendChild(tr2);
                            let th1 = document.createElement('th');
                            tr2.appendChild(th1);
                            th1.innerText = '评论id';
                            let td1 = document.createElement('td');
                            tr2.appendChild(td1);
                            td1.innerText = parse[i].commentId;
                            let th2 = document.createElement('th');
                            tr2.appendChild(th2);
                            th2.innerText = '用户名';
                            let td2 = document.createElement('td');
                            tr2.appendChild(td2);
                            td2.innerText = parse[i].userName;
                            let th3 = document.createElement('th');
                            tr2.appendChild(th3);
                            th3.innerText = '内容';
                            let td3 = document.createElement('td');
                            tr2.appendChild(td3);
                            td3.innerText = parse[i].content;
                            if (parse[i].belongsCommentId !== 0) {
                                let th4 = document.createElement('th');
                                tr2.appendChild(th4);
                                th4.innerText = '回复的评论id';
                                let td4 = document.createElement('td');
                                tr2.appendChild(td4);
                                td4.innerText = parse[i].belongsCommentId;
                            }
                            let th5 = document.createElement('th');
                            tr2.appendChild(th5);
                            let td5 = document.createElement('td');
                            tr2.appendChild(td5);
                            let a1 = document.createElement('a');
                            a1.innerText = '回复';
                            a1.href = 'UserDoComment.jsp?storeId=' + storeId + '&commentId=' + parse[i].commentId;
                            td5.appendChild(a1);
                        }
                    }
                    for (let i = 0; i < parse.length; i++) {
                        if (parse[i].userIdentity === 1) {
                            let tr1 = document.createElement('tr');
                            table.appendChild(tr1);
                            tr1.innerText = '用户评论';
                            let tr2 = document.createElement('tr');
                            table.appendChild(tr2);
                            let th1 = document.createElement('th');
                            tr2.appendChild(th1);
                            th1.innerText = '评论id';
                            let td1 = document.createElement('td');
                            tr2.appendChild(td1);
                            td1.innerText = parse[i].commentId;
                            let th2 = document.createElement('th');
                            tr2.appendChild(th2);
                            th2.innerText = '用户名';
                            let td2 = document.createElement('td');
                            tr2.appendChild(td2);
                            td2.innerText = parse[i].userName;
                            let th3 = document.createElement('th');
                            tr2.appendChild(th3);
                            th3.innerText = '内容';
                            let td3 = document.createElement('td');
                            tr2.appendChild(td3);
                            td3.innerText = parse[i].content;
                            if (parse[i].belongsCommentId !== 0) {
                                let th4 = document.createElement('th');
                                tr2.appendChild(th4);
                                th4.innerText = '回复的评论id';
                                let td4 = document.createElement('td');
                                tr2.appendChild(td4);
                                td4.innerText = parse[i].belongsCommentId;
                            }
                            let th5 = document.createElement('th');
                            tr2.appendChild(th5);
                            let td5 = document.createElement('td');
                            tr2.appendChild(td5);
                            let a = document.createElement('a');
                            a.innerText = '回复';
                            a.href = 'UserDoComment.jsp?storeId=' + storeId + '&commentId=' + parse[i].commentId;
                            td5.appendChild(a);
                        }
                    }
                }
            }
        })
        $('#a1').on('click', function () {
            this.href = 'UserDoComment.jsp?storeId=' + storeId;
        })
    })
</script>
</body>
</html>
